

use ind1990 czone ind_share year if year == 1970 using "$dir\data\interim\ind_cz_weights_1970.dta", clear

drop if czone==.
drop year

reshape wide ind_share*, i(czone) j(ind1990)

merge 1:m czone using "$dir\data\cleaned\estimation_data.dta"

drop _merge

merge 1:1 czone year using "$dir\data\interim\inst_weights_ind_1970.dta"
drop _merge

*drop t20*

*want to replace the missings in the ind_share and instrument_mean

local inds  0 10 20 30 31 32 40 41 42 50 60 100 101 102 110 111 112 120 121 122 130 132 140 141 142 150 151 152 160 161 162 171 172 180 181 182 190 191 192 ///
200 201 211 212 220 221 222 230 231 241 242 250 251 252 261 262 270 271 272 280 281 282 290 291 292 300 301 310 311 312 320 321 322 331 332 340 341 342 350 ///
351 352 360 361 370 371 372 380 381 391 392 400 401 402 410 411 412 420 421 422 432 440 441 442 450 451 452 470 471 472 500 502 511 512 521 530 531 532 540 ///
541 542 550 551 552 560 561 562 571 580 581 591 592 600 601 602 610 611 612 620 621 622 623 630 631 633 641 642 650 660 670 671 672 681 682 691 700 702 710 ///
711 712 721 722 731 732 740 741 750 751 752 760 761 762 770 771 772 780 782 790 791 800 802 810 812 820 821 830 831 832 840 841 842 850 852 860 870 871 872 ///
880 881 882 890 891 892 910 932

foreach ind in `inds' {
replace ind_share`ind' = 0 if ind_share`ind' ==.
}


global endog contemp_mean_oas_std 
global inst_2000 inst_mean_ind_std_2000 
global inst_1970 inst_mean_ind_std_1970 


*need to interact the ind shares with years
foreach year in 2007 2010 2011 2012 2013 2014 2015 2016 {
foreach var of varlist ind_share* {
			gen t`year'_`var' = `var' * (year == `year')
		}
}

*want to save the postal instrument

preserve
keep czone year  t*ind_share412 ind_share412
save "$dir\data\interim\postal_inst.dta", replace
restore

*saving the bus instrument
preserve
keep czone year  t*ind_share401 ind_share401
save "$dir\data\interim\bus_inst.dta", replace
restore

preserve
keep czone year  t*ind_share821 ind_share821
save "$dir\data\interim\chiro_inst.dta", replace
restore

preserve
local inds  0 10 20 30 31 32 40 41 42 50 60 100 101 102 110 111 112 120 121 122 130 132 140 141 142 150 151 152 160 161 162 171 172 180 181 182 190 191 192 ///
200 201 211 212 220 221 222 230 231 241 242 250 251 252 261 262 270 271 272 280 281 282 290 291 292 300 301 310 311 312 320 321 322 331 332 340 341 342 350 ///
351 352 360 361 370 371 372 380 381 391 392 400 401 402 410 411 412 420 421 422 432 440 441 442 450 451 452 470 471 472 500 502 511 512 521 530 531 532 540 ///
541 542 550 551 552 560 561 562 571 580 581 591 592 600 601 602 610 611 612 620 621 622 623 630 631 633 641 642 650 660 670 671 672 681 682 691 700 702 710 ///
711 712 721 722 731 732 740 741 750 751 752 760 761 762 770 771 772 780 782 790 791 800 802 810 812 820 821 830 831 832 840 841 842 850 852 860 870 871 872 ///
880 881 882 890 891 892 910 932


local z1970 ""
local ws ""
foreach ind in `inds' {
local z1970 `z1970' t*ind_share`ind'
local ws `ws' instrument_mean_ind_1970`ind'
}

display "`z1970'"
display "`ws'"


bartik_weight,   z(`z1970') weightstub(`ws') x(contemp_mean_oas_std) y(oas_pop_pct) weight_var(czone_pop) absorb(czone) by(year)


mat beta = r(beta)
mat alpha = r(alpha)
mat G = r(G)
qui desc t*ind_share*, varlist //stores the variables into r(varlist)
global varlist = r(varlist)


clear
svmat beta
svmat alpha
svmat G

qui gen ind = ""
qui gen year = ""
qui gen temp = ""
local t = 1
foreach var in $varlist {
	if regexm("`var'", "t(.*)_ind_share(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		*qui replace temp = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

	destring ind, replace
	destring year, replace
	save "$dir\output\Rotemberg_weights_ind_oas_pop_pct_1970.dta", replace

restore

use "$dir/data/interim/occ_by_ind_weights_by_year.dta" , clear  //just grabbing the industry labels 
keep ind1990
duplicates drop ind1990, force 

ren ind1990 ind


merge 1:m ind using "$dir\output\Rotemberg_weights_ind_oas_pop_pct_1970.dta"
drop _merge


*first, lets collapse  by year and look at which inds are contributing the most

collapse (mean) alpha1 beta1 G1, by(ind)

sort alpha1
gen alpha1_mag = abs(alpha1)

drop if ind == 0 //this is a catchall for missing industry

drop if alpha1_mag ==.

sort alpha1_mag


keep if _n>_N-10
keep ind alpha1_mag

gen temp = round(alpha1_mag, 0.01)
replace alpha1_mag = temp
drop temp

export excel  using "$dir\output\TableA7panelA.xls", replace


*repeat for the 2000 instrument
use ind1990 czone ind_share year if year == 2000 using  "$dir\data\interim\ind_cz_weights_2000.dta", clear

drop if czone==.
drop year

reshape wide ind_share*, i(czone) j(ind1990)

merge 1:m czone using "$dir\data\cleaned\estimation_data.dta"

drop _merge

*drop t20*

merge 1:1 czone year using "$dir\data\interim\inst_weights_ind_2000.dta"
drop _merge

*want to replace the missings in the ind_share and instrument_mean

local inds   10 11 12 20 31 32 40 41 42 50 60 100 101 102 110 111 112 120 121 122 130 132 140 141 142 151 152 160 161 162 171 172 180 181 182 190 191 192 200 ///
 201 210 211 212 221 222 230 231 232 242 250 251 252 261 262 270 271 272 280 281 282 291 292 300 301 310 311 312 320 322 331 332 340 341 342 351 352 360 ///
 361 362 370 371 372 390 391 392 400 401 402 410 411 412 420 421 432 440 441 450 451 452 470 471 472 500 501 502 510 511 512 521 530 531 532 540 541 542 ///
 550 551 552 560 561 562 571 580 581 582 591 600 601 610 611 612 620 621 622 623 630 631 632 633 640 641 642 650 651 652 660 661 662 663 670 671 672 681 ///
 682 691 700 701 702 710 711 712 721 722 731 732 740 741 742 750 751 752 760 761 762 770 771 772 780 781 782 791 800 801 802 810 812 820 821 822 831 832 ///
 840 841 842 850 852 860 861 862 870 871 872 873 880 881 882 890 891 892 893 901 910 921 922 930 931 932 940 941 942 950 951 952 960

foreach ind in `inds' {
replace ind_share`ind' = 0 if ind_share`ind' ==.
}

global endog contemp_mean_oas_std 
global inst_2000 inst_mean_ind_std_2000 
global inst_1970 inst_mean_ind_std_1970 


*need to interact the ind shares with years
foreach year in 2007 2010 2011 2012 2013 2014 2015 2016 {
foreach var of varlist ind_share* {
			gen t`year'_`var' = `var' * (year == `year')
		}
}





preserve
local inds   10 11 12 20 31 32 40 41 42 50 60 100 101 102 110 111 112 120 121 122 130 132 140 141 142 151 152 160 161 162 171 172 180 181 182 190 191 192 200 ///
 201 210 211 212 221 222 230 231 232 242 250 251 252 261 262 270 271 272 280 281 282 291 292 300 301 310 311 312 320 322 331 332 340 341 342 351 352 360 ///
 361 362 370 371 372 390 391 392 400 401 402 410 411 412 420 421 432 440 441 450 451 452 470 471 472 500 501 502 510 511 512 521 530 531 532 540 541 542 ///
 550 551 552 560 561 562 571 580 581 582 591 600 601 610 611 612 620 621 622 623 630 631 632 633 640 641 642 650 651 652 660 661 662 663 670 671 672 681 ///
 682 691 700 701 702 710 711 712 721 722 731 732 740 741 742 750 751 752 760 761 762 770 771 772 780 781 782 791 800 801 802 810 812 820 821 822 831 832 ///
 840 841 842 850 852 860 861 862 870 871 872 873 880 881 882 890 891 892 893 901 910 921 922 930 931 932 940 941 942 950 951 952 960



local z1970 ""
local ws ""
foreach ind in `inds' {
local z1970 `z1970' t*ind_share`ind'
local ws `ws' instrument_mean_ind_2000`ind'
}

di `ws'




bartik_weight,   z(`z1970') weightstub(`ws') x(contemp_mean_oas_std) y(oas_pop_pct) weight_var(czone_pop) absorb(czone) by(year)


mat beta = r(beta)
mat alpha = r(alpha)
mat G = r(G)
qui desc t*ind_share*, varlist //stores the variables into r(varlist)
global varlist = r(varlist)


clear
svmat beta
svmat alpha
svmat G

qui gen ind = ""
qui gen year = ""
qui gen temp = ""
local t = 1
foreach var in $varlist {
	if regexm("`var'", "t(.*)_ind_share(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		*qui replace temp = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

	destring ind, replace
	destring year, replace
	save "$dir\output\Rotemberg_weights_ind_oas_pop_pct_2000.dta", replace

restore

use "$dir/data/interim/occ_by_ind_weights_by_year.dta" , clear  //just grabbing the industry labels 
keep ind1990
duplicates drop ind1990, force 

ren ind1990 ind


merge 1:m ind using "$dir\output\Rotemberg_weights_ind_oas_pop_pct_2000.dta"
drop _merge


*first, lets collapse  by year and look at which inds are contributing the most

collapse (mean) alpha1 beta1 G1, by(ind)


sort alpha1
gen alpha1_mag = abs(alpha1)

drop if ind == 0 //this is a catchall for missing industry

drop if alpha1_mag ==.

sort alpha1_mag


keep if _n>_N-10
keep ind alpha1_mag

gen temp = round(alpha1_mag, 0.01)
replace alpha1_mag = temp
drop temp

export excel  using "$dir\output\TableA7panelB.xls", replace
